console.log(Promise);
var httpc = require('http');
var network = require('network');
var net = network.openNetWorkClient();

var request_url = 'http://appengine.oss-cn-hangzhou.aliyuncs.com/httpTest.txt';

function test_setTimeout() {
    return new Promise(function (resolve, reject) {
        console.log('=================setTimeout');
        setTimeout(function () {
            console.log('=================setTimeout---------------done');
            resolve("");
        }, 1000);
    });
}

function test_http() {
    return new Promise(function (resolve, reject) {
        console.log('=================http');
        httpc.request({
            url: request_url,
            method: 'GET',
            timeout: 1000,
            headers: {
                'content-type': 'application/json',
            },
            success: function (data) {
                console.log('http: [debug] receive data is ' + data);
                resolve(data);
            }
        });
    });
}

function testPromise()
{
    test_setTimeout()
    .then(function () {
        // setInterval异步测试返回
        console.log('xxxxxxxxxxxxx')
        return test_http();
    })
    .then(function (data) {
        // http异步测试返回
        console.log("receive http data：", data);
    });
}

var status = net.getStatus();
if (status == 'connect') {
    testPromise();
} else {
    net.on('connect', function() {
        testPromise();
    })
}